import React, { Component } from "react";
import { Redirect} from "react-router-dom";
class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userName: "",
      loginFlag: false
    };
  }
  dologin = (e) => {
    e.preventDefault();
    let userName = this.refs.userName.value;
    let password = this.refs.password.value;
    if (userName == "admin" && password == "123456") {
      this.setState({
        loginFlag: true
    });
    } else {
      alert("登录失败");
    }
  };
  render() {
    if (this.state.loginFlag) {
      return <Redirect to={{ pathname: "/" }} />;
    }
    return (
      <form onSubmit={this.dologin}>
        <input type="text" ref="userName" /> <br />
        <input type="password" ref="password" /> <br />
        <input type="submit" value="执行登录" />
      </form>
    );
  }
}

export default Login;
